﻿using LeanApp.Bll;
using System.Collections.Generic;
using System.Data;
using System.Windows.Controls;

namespace LeanApp.View
{
    /// <summary>
    /// CumulativeDataCtrl.xaml 的交互逻辑
    /// </summary>
    public partial class CumulativeDataCtrl : UserControl
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="title">标题</param>
        /// <param name="bll">业务</param>
        public CumulativeDataCtrl(string title, CumulativeBll bll)
        {
            InitializeComponent();
            _title = title;
            _bll = bll;
            InitialData();
        }

        /// <summary>
        /// 初始化数据
        /// </summary>
        private void InitialData()
        {
            title.Text = _title;
            if (_bll != null && _bll.SourceData != null && _bll.SourceData.Count > 0)
            {
                List<string> colMapper = _bll.GetColMapper();
                DataTable dt = new DataTable();
                foreach (string key in colMapper)
                {
                    if (!dt.Columns.Contains(key))
                    {
                        dt.Columns.Add(key);
                    }
                }
                _bll.SourceData.ForEach(a =>
                {
                    DataRow dr = dt.NewRow();
                    foreach (string key in colMapper)
                    {
                        if (a.ContainsKey(key))
                        {
                            dr[key] = a[key];
                        }
                    }
                    dt.Rows.Add(dr);
                });
                grid.ItemsSource = null;
                grid.ItemsSource = dt.DefaultView;
            }
        }

        /// <summary>
        /// 累积分析的业务
        /// </summary>
        private CumulativeBll _bll = null;

        /// <summary>
        /// 标题
        /// </summary>
        private string _title = "";
    }
}
